package com.yanyeori.mongodb.dao.mongodb.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.yanyeori.framework.core.util.JacksonUtil;
import com.yanyeori.mongodb.dao.mongodb.BankDao;
import com.yanyeori.mongodb.document.Bank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 增删改查 MongoDB 接口实现
 * @date 20/08/13 17:29
 */
@Service
public class BankDaoImpl implements BankDao {
    private static final Logger logger = LoggerFactory.getLogger(BankDaoImpl.class);

    /**
     * Spring Data Mongo提供了一个 MongoTemplate
     * 类似于Spring的设计的类JdbcTemplate
     */
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Bank bank) {
        mongoTemplate.save(bank);
        logger.info("保存文档成功，bank={}", JacksonUtil.toJson(bank));
    }

    @Override
    public void saveBatch(List<Bank> bankList) {
        long a = System.currentTimeMillis();
        mongoTemplate.insertAll(bankList);
        long b = System.currentTimeMillis();
        logger.info("批量保存bankList文档成功，size={}，耗时={}ms", bankList.size(), b - a);
    }

    @Override
    public void update(Bank bank) {
        Query query = new Query(Criteria.where("id").is(bank.getId()));
        Update update = new Update();
        update.set("bankId", bank.getBankId());
        update.set("bankName", bank.getBankName());
        update.set("updateTime", bank.getUpdateTime());
        //updateFirst 更新查询返回结果集的第一条
        UpdateResult result = mongoTemplate.updateFirst(query, update, Bank.class);
        //updateMulti 更新查询返回结果集的全部
        //result = mongoTemplate.updateMulti(query, update, Bank.class);
        //upsert 更新对象不存在则去添加
        //result = mongoTemplate.upsert(query, update, Bank.class);
        logger.info("更新bank={}的文档，UpdateResult={}", JacksonUtil.toJson(bank), JacksonUtil.toJson(result));
    }

    @Override
    public Bank findById(Long id) {
        Query query = new Query(Criteria.where("id").is(id));
        Bank bank = mongoTemplate.findOne(query, Bank.class);
        logger.info("查询id={}的文档，Result={}", id, JacksonUtil.toJson(bank));
        return bank;
    }

    @Override
    public Bank findByBankId(String bankId) {
        Query query = new Query(Criteria.where("bankId").is(bankId));
        Bank bank = mongoTemplate.findOne(query, Bank.class);
        logger.info("查询bankId={}的文档，Result={}", bankId, JacksonUtil.toJson(bank));
        return bank;
    }

    @Override
    public List<Bank> findAll() {
        long a = System.currentTimeMillis();
        List<Bank> bankList = mongoTemplate.findAll(Bank.class);
        long b = System.currentTimeMillis();
        logger.info("查询所有文档，size={}，耗时={}ms，Result={}", bankList.size(), b - a, JacksonUtil.toJson(bankList.size() < 100 ? bankList : bankList.subList(0, 100)));
        return bankList;
    }

    @Override
    public List<Bank> findByBankNameLike(String bankName) {
        Pattern pattern = Pattern.compile("^.*" + bankName + ".*$", Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("bankName").regex(pattern));
        List<Bank> bankList = mongoTemplate.find(query, Bank.class);
        //查询并删除
        //List<Bank> bankList = mongoTemplate.findAllAndRemove(query, Bank.class);
        logger.info("根据bankName={}模糊查询所有文档，size={}，Result={}", bankName, bankList.size(), JacksonUtil.toJson(bankList));
        return bankList;
    }

    @Override
    public DeleteResult removeById(Long id) {
        Bank bank = new Bank();
        bank.setId(id);
        DeleteResult result = mongoTemplate.remove(bank);
        logger.info("删除id={}的文档，DeleteResult={}", id, JacksonUtil.toJson(result));
        return result;
    }

    @Override
    public DeleteResult remove(Bank bank) {
        DeleteResult result = mongoTemplate.remove(bank);
        logger.info("删除文档对象，DeleteResult={}", JacksonUtil.toJson(result));
        return result;
    }

    @Override
    public void removeCollection(String collectionName) {
        mongoTemplate.dropCollection(collectionName);
        logger.info("删除集合{}", collectionName);
    }
}
